﻿module Sustainalytics.News.Admin {

    export class NewsAvailabilityService implements INewsAvailabilityService {

        private $q: ng.IQService;
        private items: NewsAvailability[];

        constructor($q: ng.IQService) {

            this.$q = $q;

            this.items =
            [
                { id: 1, name: 'All' },
                { id: 2, name: 'Client' },
                { id: 3, name: 'Prospect' }
            ];
        }

        // get news availability by ID
        public getNewsAvailability(id: number): ng.IPromise<NewsAvailability> {
            var def: ng.IDeferred<NewsAvailability> = this.$q.defer();

            var item: NewsAvailability = null;
            $(this.items).each(function () {
                if (this.id == id) {
                    item = this;
                    return false;
                }
            });

            def.resolve(item);

            return def.promise;
        }

        // get news availabilities
        public getNewsAvailabilities(): ng.IPromise<NewsAvailability[]> {
            var def: ng.IDeferred<NewsAvailability[]> = this.$q.defer();
            def.resolve(this.items);
            return def.promise;
        }
    }

    // register service with News Angular module
    var mod = angular.module(newsAdminModuleName);
    
    mod.service('NewsAvailabilityService',
        ['$q', NewsAvailabilityService]);
}